﻿using System;
using System.Collections.Generic;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Split
{
    /// <summary>
    /// 录入结果 分行 分列
    /// 由一个字符串变成list list 模样
    /// 
    /// </summary>
    public class SplitResult :ISplit
    {
        private CategoryConfig _categoryConfig = null;

        public SplitResult(CategoryConfig categoryConfig)
        {
            _categoryConfig = categoryConfig;
        }


        /// <summary>
        /// 分行
        /// </summary>
        /// <param name="entryResult"></param>
        /// <returns></returns>
        private List<string> GetRowSplitResult(string entryResult)
        {
            return entryResult.Split(new string[]{@"\\"},StringSplitOptions.None).ToList();
        }

        /// <summary>
        /// 分行 分列
        /// </summary>
        /// <param name="entryResult"></param> 
        /// <returns></returns>
        public List<List<string>> GetSplitResult(string entryResult)
        {

            List<List<string>> finalResult = null;
            if (_categoryConfig.Columns > 0)
            {
                finalResult = GetSpecialSplitResult(entryResult);
            }
            else
            {
                finalResult = GetOrangelSplitResult(entryResult);
            }

            return finalResult;
        }

       
        /// <summary>
        /// 每行结果 \\分割
        /// 每列结果\\ 分割
        /// </summary>
        /// <param name="entryResult"></param>
        /// <returns></returns>
        private List<List<string>> GetSpecialSplitResult(string entryResult)
        {


            List<List<string>> finalResult = new List<List<string>>();

            List<string> splitResult = GetRowSplitResult(@entryResult).ToList();
            
            List<string> everyRow = new List<string>();

            int count = 0;
            foreach (string splitresult in splitResult)
            {
                
                everyRow.Add( splitresult );
                count++;

                if ( count % _categoryConfig.Columns == 0)
                {

                    finalResult.Add(GetRowFinalResult(everyRow.ToArray()));
                    everyRow.Clear();
                }

            }

            if (everyRow.Count > 0)
            {
                int addi = (_categoryConfig.Columns - everyRow.Count);

                for (int i = 0; i < addi; i++)
                {
                    everyRow.Add(string.Empty);
                }

                finalResult.Add(GetRowFinalResult(everyRow.ToArray()));

            }


            return finalResult;
        }

        /// <summary>
        /// 每行结果 \\分割
        /// 每列结果\ 分割
        /// </summary>
        /// <param name="entryResult"></param>
        /// <returns></returns>
        private List<List<string>> GetOrangelSplitResult(string entryResult)
        {
           
            List<List<string>> finalResult = new List<List<string>>();

            List<string> rowResult = GetRowSplitResult(entryResult).ToList();

            foreach (var rowresult in rowResult)
            {
                string[] arrRowResult = rowresult.Split(new string[] { @"/" }, StringSplitOptions.None);

                try
                {
                    var rowFilalResult = GetRowFinalResult(arrRowResult);
                    if (rowFilalResult != null)
                        finalResult.Add(rowFilalResult);
                }
                catch (Exception ex)
                {
                    Log.Console.Error("SplitResult.GetOrangelSplitResult error", ex);
                    Log.Console.Error(string.Format("SplitResult.GetOrangelSplitResult error detail Result:{0}  fromat:{1} categoryid:{2}", rowresult, _categoryConfig.DataFormat ,_categoryConfig.CategoryId));

                }
         
            }

            return finalResult;
        }

        /// <summary>
        /// 每行结果 重新分组 根据splitformat
        /// </summary>
        /// <param name="arrRowResult"></param>
        /// <returns></returns>
        private List<string> GetRowFinalResult(string[] arrRowResult)
        {
            int splitCount = _categoryConfig.SplitFormat.Split(new char[] {'/'}).Count();
            int arrayCount = arrRowResult.Length;

            if (splitCount <= arrayCount)
            {
                string foramtRsult = string.Format(_categoryConfig.SplitFormat, arrRowResult);
                List<string> result = GetRowSplitResult(foramtRsult).ToList();
              
                return result.Select(Util.CharacterChange.ToDbc).ToList();
            }
            else
            {
                return null;
            }
        }
        
    }
}
